package cn.com.nes.site.service.distribution.impl;

import cn.com.nes.mybatis.agent.distribution.entity.CompanyApply;
import cn.com.nes.mybatis.agent.distribution.mapper.CompanyApplyMapper;
import cn.com.nes.mybatis.agent.distribution.mapper.DistributionManagementMapper;
import cn.com.nes.mybatis.agent.system.entity.Company;
import cn.com.nes.mybatis.agent.system.mapper.CompanyMapper;
import cn.com.nes.mybatis.agent.system.mapper.TAgentFacturerMapper;
import cn.com.nes.site.entity.PageData;
import cn.com.nes.site.entity.vo.distribution.*;
import cn.com.nes.site.module.dao.BaseDaoImpl;
import cn.com.nes.site.service.distribution.DistributionManagementService;
import cn.com.nes.site.service.distribution.dto.GetCompanyApplyDto;
import cn.com.nes.site.service.distribution.dto.GetMyAgentManufactureChildCompanyDto;
import cn.com.nes.site.service.equipment.archives.dto.InstallProtocalPageDto;
import cn.com.nes.site.service.system.company.ManufactureService;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class DistributionManagementServiceImpl extends BaseDaoImpl implements DistributionManagementService {

    @Resource
    private DistributionManagementMapper distributionManagementMapper;

    @Resource
    private CompanyApplyMapper companyApplyMapper;

    @Resource
    private CompanyMapper companyMapper;

    @Resource
    private TAgentFacturerMapper tAgentFacturerMapper;

    @Resource
    private ManufactureService manufactureService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public PageData deleteDistributionRelationship(DeleteDistributionRelationshipVo vo) throws Exception {
        tAgentFacturerMapper.updateToDelete(vo);
        return PageData.ok();
    }

    @Override
    public PageData getMyAgentManufactureChildCompany(GetMyAgentManufactureChildCompanyVo vo) throws Exception {
        final Page<InstallProtocalPageDto> page = new Page<>(vo.getPageid(), vo.getPagesize());
        List<GetMyAgentManufactureChildCompanyDto> manufactureChildCompanyPage = distributionManagementMapper.getMyAgentManufactureChildCompanyPage(page, vo);
        return PageData.ok(page, manufactureChildCompanyPage);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public PageData addCompanyApply(AddCompanyApplyVo vo) throws Exception {
        CompanyApply companyApply = vo.convertToCompanyApply();
        companyApply.setIsAudit(0);
        companyApplyMapper.insert(companyApply);
        return PageData.ok();
    }

    @Override
    public PageData getCompanyApplyPage(GetCompanyApplyVo vo) throws Exception {
        final Page<GetCompanyApplyDto> page = new Page<>(vo.getPageid(), vo.getPagesize());
        List<GetCompanyApplyDto> getCompanyApplyDtoList = distributionManagementMapper.getCompanyApplyPage(page, vo);
        return PageData.ok(page, getCompanyApplyDtoList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public PageData auditCompanyApply(AuditCompanyApplyVo vo) throws Exception {
        Integer isAudit = vo.getIsAudit();
        if (isAudit == 2 && StrUtil.isEmpty(vo.getIsAuditDesc())) {
            return PageData.error("审核原因不能为空！");
        }
        companyApplyMapper.updateById(vo.convertToCompanyApply());
        if (isAudit == 1) {
            checkPassToSaveCompany(vo);
        }
        return PageData.ok();
    }


    /**
     * 审核通过后创建公司
     * @param vo
     */
    private void checkPassToSaveCompany(AuditCompanyApplyVo vo) throws Exception {
        CompanyApply companyApply = companyApplyMapper.selectById(vo.getId());
        List<Company> existCompanyList = companyMapper.getExistCompany(companyApply.getCompanyFullName(), companyApply.getUniformSocialCreditCode(), companyApply.getOrganizationCode());
        if (CollUtil.isNotEmpty(existCompanyList)) {
            throw new Exception("公司已存在，不能通过申请！");
        }
        Company company = new Company();
        company.setPCompanyId("0000");
        company.setPCompanyname("北京创今世纪科技有限公司");
        company.setCompanytype(companyApply.getCompanyType());
        company.setCompanyid(getNextId(company.getPCompanyId()));
        company.setSfsc(0);
        company.setGmtCreate(vo.getOperateDate());
        company.setGmtCreateId(vo.getOperateUserId());
        company.setCompanyname(companyApply.getCompanyFullName());
        company.setShortname(companyApply.getCompanyShortName());
        company.setLegalperson(companyApply.getLegalPerson());
        company.setNesCompanyid(company.getCompanyid());
        company.setUniformsocialcreditcode(companyApply.getUniformSocialCreditCode());
        company.setOrganizationCode(companyApply.getOrganizationCode());
        company.setContact(companyApply.getContact());
        company.setContactnumber(companyApply.getContactNumber());
        company.setAreacode(companyApply.getAreaCode());
        company.setAddress(companyApply.getRegisterAddress());
        companyMapper.insert(company);
        manufactureService.saveOpeCompany(company, vo.getUserInfo().getUsername(), true);
    }

}
